package cz.data.domain.metadata.explore;

import cz.data.common.support.db.core.DbColumn;
import cz.data.common.support.db.core.DbTable;
import cz.data.domain.metadata.model.entity.MetadataColumnEntity;
import cz.data.domain.metadata.model.entity.MetadataSourceEntity;
import cz.data.domain.metadata.model.entity.MetadataTableEntity;
import cz.data.domain.metadata.service.MetadataColumnService;
import cz.data.domain.metadata.service.MetadataTableService;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;
import org.springframework.context.annotation.Lazy;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

public abstract class AbstractMetadataExplore {

    @Lazy
    @Resource
    protected MetadataTableService tableService;
    @Lazy
    @Resource
    protected MetadataColumnService columnService;

    protected boolean refreshTable(MetadataTableEntity table) {
        MetadataTableEntity dbTable = tableService.getMetadataTableBySourceIdAndTableName(table.getSourceId(), table.getTableName());
        if (Objects.isNull(dbTable)) {
            if (StringUtils.isBlank(table.getTableComment())) {
                table.setTableComment(table.getTableName() + "表未描述");
            }
            return tableService.save(table);
        } else {
            table.setId(dbTable.getId());
            if (StringUtils.isBlank(table.getTableComment())) {
                if (StringUtils.isNotBlank(dbTable.getTableComment())) {
                    table.setTableComment(dbTable.getTableComment());
                } else {
                    table.setTableComment(table.getTableName() + "表未描述");
                }
            }
            return tableService.updateById(table);
        }
    }

    protected boolean refreshColumn(MetadataColumnEntity column) {
        MetadataColumnEntity dbColumn = columnService.getMetadataColumnByTableIdAndColumnName(column.getTableId(), column.getColumnName());
        if (Objects.isNull(dbColumn)) {
            if (StringUtils.isBlank(column.getColumnComment())) {
                column.setColumnComment(column.getColumnName() + "列未描述");
            }
            return columnService.save(column);
        } else {
            column.setId(dbColumn.getId());
            if (StringUtils.isBlank(column.getColumnComment())) {
                if (StringUtils.isNotBlank(dbColumn.getColumnComment())) {
                    column.setColumnComment(dbColumn.getColumnComment());
                } else {
                    column.setColumnComment(column.getColumnName() + "列未描述");
                }
            }
            return columnService.updateById(column);
        }
    }

    public abstract MetadataSourceEntity explore(MetadataSourceEntity dataSource);

    protected List<MetadataTableEntity> convertTables(List<DbTable> tables, String sourceId) {
        return tables.stream().map(table -> {
            MetadataTableEntity metadataTable = new MetadataTableEntity();
            metadataTable.setSourceId(sourceId);
            metadataTable.setTableName(table.getTableName());
            metadataTable.setTableComment(table.getTableComment());
            return metadataTable;
        }).collect(Collectors.toList());
    }

    protected List<MetadataColumnEntity> convertColumns(List<DbColumn> columns, String sourceId, String tableId) {
        return columns.stream().map(column -> {
            MetadataColumnEntity metadataColumn = new MetadataColumnEntity();
            metadataColumn.setSourceId(sourceId);
            metadataColumn.setTableId(tableId);
            metadataColumn.setColumnName(column.getColName());
            metadataColumn.setColumnComment(column.getColComment());
            metadataColumn.setColumnKey(column.getColKey() ? "1" : "0");
            metadataColumn.setColumnNullable(Boolean.TRUE.equals(column.getNullable()) ? "1" : "0");
            Integer position = column.getColPosition();
            metadataColumn.setColumnPosition(Objects.isNull(position) ? "0" : position.toString());
            metadataColumn.setDataType(column.getDataType());
            metadataColumn.setDataLength(column.getDataLength());
            metadataColumn.setDataPrecision(column.getDataPrecision());
            metadataColumn.setDataScale(column.getDataScale());
            metadataColumn.setDataDefault(column.getDataDefault());
            return metadataColumn;
        }).collect(Collectors.toList());
    }
}
